package com.xj.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @version 1.0
 * @program: demo2
 * @packageName: com.xj.config
 * @className JedisConfig
 * @description: TODO
 * @author: xj
 * @create: 2021-01-08 16:51:37
 **/
@Configuration
public class RedissonConfig {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.database}")
    private int database;



    @Bean
    public RedissonClient getRedisson() {
        Config config = new Config();
        if ("".equals(password)) {
            password = null;
        }

        //单机模式 依次设置redis地址和密码
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
                .setPassword(password)
                .setDatabase(database)
        ;
        //设置序列化
        config.setCodec(new StringCodec());
        return Redisson.create(config);
    }

}
